import sys
import os

# 将项目根目录添加到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import app, db
from backend.models.user import User
from datetime import datetime

# 创建测试用户
with app.app_context():
    # 检查是否已存在测试用户
    existing_user = User.query.filter_by(phone='13800138000').first()
    
    if not existing_user:
        # 创建新的测试用户
        test_user = User(phone='17325964597')
        test_user.set_password('123456')  # 设置密码
        test_user.username = '测试用户'
        test_user.last_login_at = datetime.utcnow()
        
        # 添加到数据库
        db.session.add(test_user)
        db.session.commit()
        
        print("测试用户创建成功!")
        print("手机号: 17325964597")
        print("密码: 123456")
    else:
        print("测试用户已存在，无需创建。")
        print("手机号: 17325964597")
        print("密码: 123456")

    # 为了方便测试微信登录，我们也可以创建一个关联了微信openid的用户
    wechat_user = User.query.filter_by(phone='13800138001').first()
    
    if not wechat_user:
        wechat_test_user = User(phone='13800138001')
        wechat_test_user.set_password('123456')
        wechat_test_user.username = '微信测试用户'
        wechat_test_user.wechat_openid = 'test_openid_123456'
        wechat_test_user.last_login_at = datetime.utcnow()
        
        db.session.add(wechat_test_user)
        db.session.commit()
        
        print("\n微信测试用户创建成功!")
        print("手机号: 13800138001")
        print("密码: 123456")
        print("OpenID: test_openid_123456")
    else:
        print("\n微信测试用户已存在，无需创建。")
        print("手机号: 13800138001")
        print("密码: 123456")
        print("OpenID: test_openid_123456")
